package com.ilock.action;

import java.util.HashMap;
import java.util.Map;

import com.ilock.bean.Ilockinfo;
import com.ilock.bean.Ilockstate;
import com.ilock.bean.Iuser;
import com.ilock.bean.Templockop;
import com.ilock.service.*;
import com.opensymphony.xwork2.ActionSupport;

public class LockOpAction extends ActionSupport{
	private IUserService IUserService;
	private ILockService ILockService;
	private ILockLogService ILockLogService;
	private ILockStateService ILockStateService;
	private String useremail;
	private String userpassword;
	private String locktoken;
	private int operate;
	private int lockstate;
	private Map<String,Object> opMap;

	
	public String getUseremail() {
		return useremail;
	}

	public void setUseremail(String useremail) {
		this.useremail = useremail;
	}

	public String getUserpassword() {
		return userpassword;
	}

	public void setUserpassword(String userpassword) {
		this.userpassword = userpassword;
	}

	public String getLocktoken() {
		return locktoken;
	}

	public void setLocktoken(String locktoken) {
		this.locktoken = locktoken;
	}

	public int getOperate() {
		return operate;
	}

	public void setOperate(int operate) {
		this.operate = operate;
	}

	public void setIUserService(IUserService iUserService) {
		IUserService = iUserService;
	}

	public void setILockService(ILockService iLockService) {
		ILockService = iLockService;
	}
	
	

	public IUserService getIUserService() {
		return IUserService;
	}

	public ILockService getILockService() {
		return ILockService;
	}

	public ILockLogService getILockLogService() {
		return ILockLogService;
	}

	public void setILockLogService(ILockLogService iLockLogService) {
		ILockLogService = iLockLogService;
	}

	public ILockStateService getILockStateService() {
		return ILockStateService;
	}

	public void setILockStateService(ILockStateService iLockStateService) {
		ILockStateService = iLockStateService;
	}

	public Map<String, Object> getOpMap() {
		return opMap;
	}

	public void setOpMap(Map<String, Object> opMap) {
		this.opMap = opMap;
	}

	public int getLockstate() {
		return lockstate;
	}

	public void setLockstate(int lockstate) {
		this.lockstate = lockstate;
	}

	public String lockOp()throws Exception{
		opMap = new HashMap<String,Object>();
		Iuser tempiuser = checkUser();
		Map<String,Object> tempmap = new HashMap<String,Object>();
		if(tempiuser!=null){
			Ilockinfo ilockinfo = ILockService.findLockInfoByToken(locktoken);
			if(ilockinfo!=null){
				Ilockstate ilockstate = ILockStateService.getLockStateById(ilockinfo.getIlockInfoNumber());
				if(ilockstate.getState()!=operate){
					ILockService.deleteTempLockOp(ilockinfo.getIlockInfoNumber());
					ILockService.addTempLockOp(tempiuser.getIuserNumber(), ilockinfo.getIlockInfoNumber(), operate);
					tempmap.put("state", "operate:"+operate+"record done.");
//					tempmap.put("tempLockOp", tempLockOp);
				}else{
					tempmap.put("state", "duplicate operation");
				}
//				Templockop tempLockOp = ILockService.findTempLockOp(ilockinfo.getIlockInfoNumber());
//				if(tempLockOp!=null && tempLockOp.getOperate()!=operate){
//					ILockService.updateTempLockOp(tempLockOp.getIlockInfoNumber());
//					tempmap.put("state", "operate:"+operate+" done.");
//					tempmap.put("tempLockOp", tempLockOp);
//				}else{
//					tempmap.put("state", "duplicate operation");
//				}
			}else{
				tempmap.put("state", "token error");
			}
		}else{
			tempmap.put("state", "user info error.");
		}
		this.setOpMap(tempmap);
		return "success";
	}

	public String findOp(){
		opMap = new HashMap<String,Object>();
		Ilockinfo ilockinfo = ILockService.findLockInfoByToken(locktoken);
		if(ilockinfo!=null){
			Templockop templockop = ILockService.findTempLockOp(ilockinfo.getIlockInfoNumber());
			if(templockop!=null){
				opMap.put("lockop", templockop);
				opMap.put("state", "operate:findOp done");
			}else{
				opMap.put("state", "record empty");
			}
		}else{
			opMap.put("state", "token error");
		}
		return "success";
	}
	
	public String updateLockState()throws Exception{
		opMap = new HashMap<String,Object>();
		Ilockinfo ilockinfo = ILockService.findLockInfoByToken(locktoken);
		if(ilockinfo!=null){
			Ilockstate templockstate =ILockStateService.getLockStateById(ilockinfo.getIlockInfoNumber());
			Templockop templockop = ILockService.findTempLockOp(ilockinfo.getIlockInfoNumber());
			opMap.put("lockop", templockop);
			if(templockstate!=null){
				templockstate.setState(lockstate);
				ILockStateService.updateLockState(templockstate);
				opMap.put("state", "operation:lock state update done");
			}else{
				opMap.put("state", "lock state error");
			}	
		}else{
			opMap.put("state", "token error");
		}
		return "success";
	}
	
	public String findLockState()throws Exception{
		opMap = new HashMap<String,Object>();
		Ilockinfo ilockinfo = ILockService.findLockInfoByToken(locktoken);
		if(ilockinfo!=null){
			Ilockstate templockstate =ILockStateService.getLockStateById(ilockinfo.getIlockInfoNumber());
			if(templockstate!=null){
				opMap.put("lockstate", templockstate);
				opMap.put("state", "operation:lock state find done");
			}else{
				opMap.put("state", "lock state empty");
			}	
		}else{
			opMap.put("state", "token error");
		}
		return "success";
	}
//	public String findOp()throws Exception{
//		Iuser tempiuser = checkUser();
//		Map<String,Object> tempmap = new HashMap<String,Object>();
//		if(tempiuser!=null){
//			Ilockinfo ilockinfo = ILockService.findLockInfoByToken(locktoken);
//			if(ilockinfo!=null){
//				Templockop tempLockOp = ILockService.findTempLockOp(ilockinfo.getIlockInfoNumber());
//				tempmap.put("tempLockOp", tempLockOp);
//				tempmap.put("state", "operate:findOp done.");
//			}else{
//				tempmap.put("state", "token error");
//			}
//		}else{
//			tempmap.put("state", "user info error.");
//		}
//		this.setOpMap(tempmap);
//		return "success";
//	}
	public String execute()throws Exception{

		return "success";
	}

	public Iuser checkUser(){
		Iuser iuser = null;
		iuser=IUserService.checkIn(useremail, userpassword);
		return iuser;
	}
}
